<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>{{ids?'编辑':'新增'}}审核模版</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form label-width="90px" ref="editForm">
          <el-form-item label="模版名称：" prop="num">
            <el-input v-model="title" placeholder="填写模版名称" size="small"></el-input>
          </el-form-item>
        </el-form>
        <el-table
            :data="examineList"
            border
            style="width: 100%">
          <el-table-column
              align="center"
              label="流程名称"
              width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" auto-complete="off" placeholder="名称" size="small"></el-input>
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              label="审批人">
            <template slot-scope="scope">
              <el-button @click="getMoreTeach(scope.$index)" size="mini">选择审批人</el-button>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(scope.$index,line)"
                  class="mr-10"
                  size="small"
                  v-for="(items,line) in scope.row.dynamicTags"
                  :key="items"
              >{{ items }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
              prop="address"
              label="操作"
              align="center"
              width="70">
            <template slot-scope="scope">
              <el-button type="danger" plain size="mini" @click="detelLine(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click="saveSetting" plain>确定并保存</el-button>
        <el-button size="small" type="success" @click="addExamine" plain>新增审核流程</el-button>
      </div>
    </div>
    <MultipleTeacherModle :visible.sync="studentVisible" @studentFn="studentFn"
                          :dynamicTagsArray="dynamicTagsArray"></MultipleTeacherModle>
  </div>
</template>

<script>
import {purchaseDetail, purchaseEdit, purchaseAdd} from "@/api/admin/purchase"

export default {
  props: ['visible', 'ids'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      singleTeachVisible: false,
      dialogVisible: false,
      examine_user: {}, // 审核人
      editForm: {
        name: '', // 名称
        user_id: '' // 审核人
      }, // 编辑界面数据
      title: '',
      studentVisible: false,
      dynamicTagsArray: {},
      examineList: [],
      examinIndex: 0
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.ids) {
          this.getDetail();
        }
      } else {
        this.examineList = []
        this.dynamicTagsArray = {}
        this.title = ''
        this. examinIndex = 0
      }
    }
  },
  created() {
  },
  methods: {
    getDetail() {
      purchaseDetail({id: this.ids}).then(res => {
        console.log('res:', res)
        if (res.vo.flows) {
          for (let item of res.vo.flows) {
            item.dynamicTagsArray = {};
            item.dynamicTagsId = [];
            item.dynamicTags = [];
            for (let items of item.users) {
              item.dynamicTagsArray[items.id] = items
              item.dynamicTagsId.push(items.id)
              item.dynamicTags.push(items.realname)
            }
          }
          this.title = res.vo.title
          this.examineList = this.getJson(res.vo.flows)
        }
      })
    },
    async getSetting() {
      // let option = this.getJson(this.examineList);
      let data = await this.$fetch(this.api.purchase.setting, {id: 1});
      if (!data.code) {
        if (data.vo.flows) {
          console.log('data:', data)
          for (let item of data.vo.flows) {
            item.dynamicTagsArray = {};
            item.dynamicTagsId = [];
            item.dynamicTags = [];
            for (let items of item.users) {
              item.dynamicTagsArray[items.id] = items
              item.dynamicTagsId.push(items.id)
              item.dynamicTags.push(items.realname)
            }
          }
          this.examineList = this.getJson(data.vo.flows)
        }
      }
    },
    async saveSetting() {
      let flow = []
      for (let item of this.examineList) {
        if (!item.name) {
          this.$message.error('请填写审核名称')
          return
        }
        if (!item.dynamicTagsId.length) {
          this.$message.error('请选择审批人')
          return
        }
        flow.push({
          name: item.name,
          user_id: item.dynamicTagsId.join(',')
        })
      }
      console.log('提交的参数内容，', flow)
      if (this.ids) {
        purchaseEdit({id: this.ids, title: this.title, flow: JSON.stringify(flow)}).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success('提交成功！')
            this.$emit('getList')
            this.closePOpup()
          }
        })
      } else {
        purchaseAdd({title: this.title, flow: JSON.stringify(flow)}).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success('提交成功！')
            this.$emit('getList')
            this.closePOpup()
          }
        })
      }
    },
    getMoreTeach(index) {
      this.examinIndex = index
      this.dynamicTagsArray = this.getJson(this.examineList[index].dynamicTagsArray)
      this.studentVisible = true
    },
    studentFn(option) {
      console.log('获取教师信息L:', option)
      let optionData = this.getJson(this.examineList[this.examinIndex])
      optionData.dynamicTags = []
      optionData.dynamicTagsId = []
      optionData.dynamicTagsArray = {}
      for (let i in option) {
        optionData.dynamicTags.push(option[i].realname)
        optionData.dynamicTagsId.push(option[i].id)
        optionData.dynamicTagsArray = option
      }
      this.$set(this.examineList, this.examinIndex, optionData)
    },
    // 删除审核人
    handleCloseExamine(index, line) {
      let list = this.getJson(this.examineList[index])
      list.dynamicTags.splice(line, 1)
      delete list.dynamicTagsArray[list.dynamicTagsId[line]] // 去除选中的人
      list.dynamicTagsId.splice(line, 1)
      this.$set(this.examineList, index, list)
    },
    addExamine() {
      this.examineList.push({
        dynamicTags: [],
        dynamicTagsId: [],
        dynamicTagsArray: {},
        name: ''
      })
    },
    detelLine(index) {
      this.examineList.splice(index, 1)
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 60%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


